
package pe.org.jhsystem.consola.xml.dom;

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

/**
 *
 * @author Jose Luis Huaman Villar
 */
public class ParseXMLString {

    public static void main(String arg[]) {
        String xmlRecords =
"<transcript>" +
"    <presence id='M2C78-198' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <x xmlns='http://jabber.org/protocol/muc#user'>" +
"            <item affiliation='none' role='participant' jid='f7739abd@helpdesk.reniec.gob.pe/f7739abd'/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:17:51'/>" +
"    </presence>" +
"    <presence id='2DuXh-110' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <x xmlns='http://jabber.org/protocol/muc#user'>" +
"            <item affiliation='none' role='participant' jid='supervisora@helpdesk.reniec.gob.pe/spark'/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:17:51'/>" +
"    </presence>" +
"    <message id='2DuXh-114' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <body>Buenas tardes, </body>" +
"        <x xmlns='jabber:x:event'>" +
"            <offline/>" +
"            <delivered/>" +
"            <displayed/>" +
"            <composing/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:18:28'/>" +
"    </message>" +
"    <message id='M2C78-213' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>hola</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:20:03'/>" +
"    </message>" +
"    <message id='2DuXh-116' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <body>Que tal</body>" +
"        <x xmlns='jabber:x:event'>" +
"            <offline/>" +
"            <delivered/>" +
"            <displayed/>" +
"            <composing/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:20:30'/>" +
"    </message>" +
"    <message id='M2C78-216' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>bien gracias</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:20:43'/>" +
"    </message>" +
"    <message id='M2C78-218' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>estoy probando desde afuera el chat reniec</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:20:45'/>" +
"    </message>" +
"    <message id='2DuXh-118' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <body>que bien.. </body>" +
"        <x xmlns='jabber:x:event'>" +
"            <offline/>" +
"            <delivered/>" +
"            <displayed/>" +
"            <composing/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:21:07'/>" +
"    </message>" +
"    <message id='M2C78-232' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>este chat me parece muy util para cualquier duda</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:22:07'/>" +
"    </message>" +
"    <message id='2DuXh-120' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <body>si claro</body>" +
"        <x xmlns='jabber:x:event'>" +
"            <offline/>" +
"            <delivered/>" +
"            <displayed/>" +
"            <composing/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:22:31'/>" +
"    </message>" +
"    <message id='2DuXh-124' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <body>eres cindy de que area??</body>" +
"        <x xmlns='jabber:x:event'>" +
"            <offline/>" +
"            <delivered/>" +
"            <displayed/>" +
"            <composing/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:22:43'/>" +
"    </message>" +
"    <message id='M2C78-244' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>no soy d ningun area.. estoy haciendo pruebas de una pc externa" +
"        </body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:23:48'/>" +
"    </message>" +
"    <message id='2DuXh-125' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <body>ha ok</body>" +
"        <x xmlns='jabber:x:event'>" +
"            <offline/>" +
"            <delivered/>" +
"            <displayed/>" +
"            <composing/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:24:06'/>" +
"    </message>" +
"    <message id='2DuXh-129' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/supervisora'>" +
"        <body>si sera un servicio util.. </body>" +
"        <x xmlns='jabber:x:event'>" +
"            <offline/>" +
"            <delivered/>" +
"            <displayed/>" +
"            <composing/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:24:20'/>" +
"    </message>" +
"    <message id='M2C78-250' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>si</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:24:52'/>" +
"    </message>" +
"    <message id='M2C78-252' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>asi es</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:24:54'/>" +
"    </message>" +
"    <message id='M2C78-264' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>espero se lance pronto el servicio</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:25:55'/>" +
"    </message>" +
"    <message id='M2C78-266' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>nos vemos</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:25:57'/>" +
"    </message>" +
"    <message id='M2C78-269' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' type='groupchat' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy'>" +
"        <body>buenas tardes</body>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:26:00'/>" +
"    </message>" +
"    <presence id='M2C78-198' to='callcenter@workgroup.helpdesk.reniec.gob.pe/callcenter' from='cbq82278@conference.helpdesk.reniec.gob.pe/Cindy' type='unavailable'>" +
"        <x xmlns='http://jabber.org/protocol/muc#user'>" +
"            <item affiliation='none' role='none' jid='f7739abd@helpdesk.reniec.gob.pe/f7739abd'/>" +
"        </x>" +
"        <x xmlns='jabber:x:delay' stamp='20110414T22:32:06'/>" +
"    </presence>" +
"</transcript>";

//            "<data>" +
//            " <employee>" +
//            "   <name>John</name>" +
//            "   <title>Manager</title>" +
//            " </employee>" +
//            " <employee>" +
//            "   <name>Sara</name>" +
//            "   <title>Clerk</title>" +
//            " </employee>" +
//            "</data>";

        try {
            DocumentBuilderFactory dbf =
            DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlRecords));

            Document doc = db.parse(is);
            NodeList nodes = doc.getElementsByTagName("message");

            // iterate the message
            for (int i = 0; i < nodes.getLength(); i++) {
                Element element = (Element) nodes.item(i);
                //System.out.println("----------");
                //System.out.println("ID: " + element.getAttribute("id"));
                String from = element.getAttribute("from");
                from = from.substring(from.indexOf("/")+1,from.length());

                NodeList body = element.getElementsByTagName("body");
                Element line = (Element) body.item(0);
                String mensaje = from+": "+getCharacterDataFromElement(line);

                NodeList x = element.getElementsByTagName("x");
                String hora = "";
                for(int j = 0; j < x.getLength(); j++) {
                    line = (Element) x.item(j);
                    hora = line.getAttribute("stamp");
                    if(!hora.equals("")) j = x.getLength();
                }
                if(!hora.equals(""))
                    hora = hora.substring(0,hora.indexOf("T")) + " " + hora.substring(hora.indexOf("T")+1,hora.length());

                //System.out.println("Hora: "+hora);
                System.out.println("[" + hora + "] " + mensaje);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        /*
        output :
        Name: John
        Title: Manager
        Name: Sara
        Title: Clerk
        */
    }

    public static String getCharacterDataFromElement(Element e) {
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
        return "?";
    }
}
